package com.cloudspace.yunloan.network.api;

import com.cloudspace.yunloan.entity.BaseResult;
import com.cloudspace.yunloan.entity.LoginResult;
import com.cloudspace.yunloan.entity.PicCheckResult;
import com.cloudspace.yunloan.entity.UnderwayListResult;
import com.cloudspace.yunloan.entity.UploadMediaCallback;
import com.cloudspace.yunloan.network.JsonCallBack;
import com.cloudspace.yunloan.update.UpdateInfoResult;
import com.cloudspace.yunloan.utils.AppPreferences;
import com.lzy.okhttputils.OkHttpUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.util.List;

public class UserApi extends BaseAPI {
    public static final String TAG = UserApi.class.getSimpleName();

    public void login(Object tag, String phone, String pwd, JsonCallBack<LoginResult> cb){
        String url = getBaseApiUrl() + "logonmobile";

        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("userid", phone);
            jsonObject.put("password", pwd);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        OkHttpUtils.post(url)
                .tag(tag)
                .upJson(jsonObject.toString())
                .execute(cb);
    }

    public void valid(Object tag, String idCardNo,String name, float score, String address, List<File> files, JsonCallBack<BaseResult> cb){
        String url = getBaseApiUrl() + "createPdf";

        OkHttpUtils.post(url)
                .tag(tag)
                .headers("YK_AUTH", AppPreferences.getString("token"))
                .params("fontcard", files.get(0))
                .params("backcard", files.get(1))
                .params("borrower", files.get(2))
                .params("cardid", idCardNo)
                .params("borrowername", name)
                .params("score", score)
                .params("address", address)
                .execute(cb);
    }

    public void piccheck(Object tag,List<File> files, JsonCallBack<PicCheckResult> cb){
        String url = getUrl2Builder("mobile") + "piccheck";

        OkHttpUtils.post(url)
                .tag(tag)
                .headers("YK_AUTH", AppPreferences.getString("token"))
                .params("imgOne", files.get(0))
                .params("imgTwo", files.get(1))
                .execute(cb);
    }

    public void uploadMedia(Object tag, String cardid, File videoFile, JsonCallBack<UploadMediaCallback> cb){
        String url = getBaseApiUrl() + "uploadMedia";

        OkHttpUtils.post(url)
                .tag(tag)
                .headers("YK_AUTH", AppPreferences.getString("token"))
                .params("cardid", cardid)
                .params("video", videoFile)
                .execute(cb);
    }

    public void underway(Object tag, JsonCallBack<UnderwayListResult> cb){
        String url = getBaseApiUrl() + "underway";

        OkHttpUtils.post(url)
                .tag(tag)
                .headers("YK_AUTH", AppPreferences.getString("token"))
                .execute(cb);
    }

    public void finished(Object tag, JsonCallBack<UnderwayListResult> cb){
        String url = getBaseApiUrl() + "finished";

        OkHttpUtils.post(url)
                .tag(tag)
                .headers("YK_AUTH", AppPreferences.getString("token"))
                .execute(cb);
    }

    public void updateapp(Object tag, JsonCallBack<UpdateInfoResult> cb){
        OkHttpUtils.post(getBaseApiUrl() + "updateapp")
                .tag(tag)
                .execute(cb);
    }

    @Override
    protected String getBaseApiUrl() {
        return getUrlBuilder("mobile");
    }
}
